package Dao.imp;

import entity.ProductNote;
import mapper.ProductNoteMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import utils.SqlSessionFactoryUtils;

import java.util.List;

public class ProductNoteDao implements ProductNoteMapper {
    private SqlSessionFactory sqlSessionFactory;

    public ProductNoteDao() {
        sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    }

    @Override
    public int addProductNote(ProductNote productNote) {
        int result = -1;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            result = session.insert("addProductNote", productNote);
            session.commit();
        }
        return result;
    }

    @Override
    public int deleteProductNote(int pnid) {
        int result = -1;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            result = session.delete("deleteProductNote", pnid);
            session.commit();
        }
        return result;
    }

    @Override
    public int updateProductNote(ProductNote productNote) {
        int result = -1;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            result = session.update("updateProductNote", productNote);
            session.commit();
        }
        return result;
    }

    @Override
    public List<ProductNote> queryProductNoteByPid(int pid) {
        List<ProductNote> list = null;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            list = session.selectList("queryProductNoteByPid", pid);
        }
        return list;
    }

    @Override
    public List<ProductNote> queryAllproductNote() {
        List<ProductNote> list = null;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            list = session.selectList("queryAllproductNote");
        }
        return list;
    }

    @Override
    public int getProductNoteCount() {
        int result = -1;
        try (SqlSession session = sqlSessionFactory.openSession()) {
            result = session.selectOne("getProductNoteCount");
        }
        return result;
    }

}
